package com.fanbai.score.common.Utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;


import com.fanbai.score.common.Utils.log.YD_Log;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 6.0以后Android，需要动态申请权限
 * 
 * @author Cong
 *
 */
public class PermissionUtils {

	public final String[] GROUP_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE,
			Manifest.permission.WRITE_EXTERNAL_STORAGE };
	public final String[] GROUP_BLUETOOTH = { Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN };
	public final String[] GROUP_LOCATION = { Manifest.permission.ACCESS_FINE_LOCATION,
			Manifest.permission.ACCESS_COARSE_LOCATION };
	public final String[] GROUP_CAMERA = { Manifest.permission.CAMERA };
	public final String[] GROUP_CALL = { Manifest.permission.CALL_PHONE };

	private static PermissionUtils permissionUtils;

	private PermissionUtils() {
	}

	public static PermissionUtils getInstance() {
		if (permissionUtils == null) {
			permissionUtils = new PermissionUtils();
		}
		return permissionUtils;
	}

	/**
	 * 过滤出需要申请的权限
	 * 
	 * @param act
	 * @param group_perms
	 * @return
	 */
	public String[] filtRequestPerm(Activity act, String[]... group_perms) {
		if (Build.VERSION.SDK_INT < 23)
			return null;
		ArrayList<String> perms = new ArrayList<String>();
		for (String[] group : group_perms) {
			for (String perm : group) {
				// 无此权限时
				YD_Log.writeLog("permission", "判断是否需要申请权限：" + perm);
				if (ActivityCompat.checkSelfPermission(act, perm) != PackageManager.PERMISSION_GRANTED) {
					YD_Log.writeLog("permission", "应申请的权限：" + perm);
					perms.add(perm);
				}
			}
		}
		if (perms.size() > 0) {
			String[] permArr = new String[perms.size()];
			return perms.toArray(permArr);
		}
		return null;
	}

	public synchronized void request(Activity act, @NonNull String[] perms, int requestCode) {
		for (String perm : perms) {
			YD_Log.writeLog("permission", "申请权限：" + perm);
			if(ActivityCompat.shouldShowRequestPermissionRationale(act, perm)){
				if(useList(GROUP_BLUETOOTH, perm) || useList(GROUP_LOCATION, perm)){//蓝牙权限
					CommonUtil.checkBlePermission(act);
				}else if(useList(GROUP_CAMERA, perm)){
					CommonUtil.checkCameraPermission(act);
				}else if(useList(GROUP_CALL, perm)){
					CommonUtil.checkCallPermission(act);
				}else{
					CommonUtil.checkPermission(act);
				}
				return ;
			}
		}
		ActivityCompat.requestPermissions(act, perms, requestCode);
	}

	public synchronized void request(Fragment frag, @NonNull String[] perms, int requestCode) {
		for (String perm : perms) {
			YD_Log.writeLog("permission", "申请权限：" + perm);
			if(ActivityCompat.shouldShowRequestPermissionRationale(frag.getActivity(), perm)){
				if(useList(GROUP_BLUETOOTH, perm) || useList(GROUP_LOCATION, perm)){//蓝牙权限
					CommonUtil.checkBlePermission(frag.getActivity());
				}else if(useList(GROUP_CAMERA, perm)){
					CommonUtil.checkCameraPermission(frag.getActivity());
				}else if(useList(GROUP_CALL, perm)){
					CommonUtil.checkCallPermission(frag.getActivity());
				}else{
					CommonUtil.checkPermission(frag.getActivity());
				}
				return ;
			}
		}
		frag.requestPermissions(perms, requestCode);
	}

	/**
	 * 获取权限申请的回调(重写onRequestPermissionsResult后，在方法内调用)
	 * 
	 * @param requestCode
	 * @param grantResults
	 * @param permissionCode
	 * @param callBack
	 */
	public void onResult(int requestCode, int[] grantResults, int permissionCode, permissionCallBack callBack) {
		if (requestCode == permissionCode) {
			boolean isGranted = true;
			for (int result : grantResults) {
				if (result != PackageManager.PERMISSION_GRANTED) {
					isGranted = false;
					break;
				}
			}
			callBack.onResult(isGranted);
		}
	}

	public interface permissionCallBack {
		void onResult(boolean isGranted);
	}


	public boolean useList(String[] arr, String targetValue) {
		return Arrays.asList(arr).contains(targetValue);
	}
}
